package com.haoqizhe.kernel.web.result;

import com.haoqizhe.kernel.commons.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author haoqizhe.li
 * @date 2018/4/2 23:18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel(value = "正常返回信息")
public class Result implements IResult{
    private static final long serialVersionUID = 874200365941306385L;

    @ApiModelProperty(value = "返回值代码")
    private Integer code;

    @ApiModelProperty(value = "返回提醒信息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private Object data;

    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(ResultCode resultCode, Object data) {
        Result result = new Result();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public static Result failure(String message) {
        Result result = new Result();
        result.setCode(ResultCode.ERROR.code());
        result.setMessage(message);
        return result;
    }

    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.message = code.message().replaceAll("\\{}","");
    }


}
